<template>
	<view class="container">
		<view class="white-cover">
		</view>
		<view class="cover">
		</view>


		<view class="content">
			<image src="@appstatic/user/user_prompt_login.png" mode=""
				style="width: 290.74px;height: 240.21px;margin-top: 217px;"></image>

			<view class="test1" v-if="isPopularize==false" style="margin-top: 68px;margin-bottom: 24px;">
				登录后可使用全部功能
			</view>
			<view class="test1" v-else-if="isPopularize==true" style="margin-top: 68px;margin-bottom: 24px;">
				欢迎使用派方机车
			</view>

			<view class="button_row" v-if="checked==false" @click="checkedToast">
				绑定手机号
			</view>

			<button class="button_row" v-else open-type='getPhoneNumber' @getphonenumber='getPhoneNumberconfidential'>
				绑定手机号
			</button>


			<view
				style="display: flex;flex-direction: column;justify-content: space-between;height: calc(100vh - 647px);">
				<view class="jurisdiction">
					<view>
						<checkbox-group @change="onCheckChange">
							<checkbox value="vvv" :checked="checked" style="transform: scale(0.64);"></checkbox>
						</checkbox-group>
					</view>
					<view class="checkbox-test">
						我已阅读并同意
					</view>
					<!-- <view class="text" @click="agreementTouched" style="color: #EA5404;">
					《用户服务协议》
				</view>
				<view class="checkbox-test">、</view> -->
					<view class="checkbox-test" @click="privateTouched" style="color: #EA5404;">
						《隐私政策》
					</view>
				</view>

			</view>


		</view>
	</view>
	</view>
</template>

<script>
	const app = getApp();
	const util = require("@utils/util.js");
	const network = require("@manager/network-manager.js");
	const dialog = require("@utils/dialog.js");
	export default {
		data() {
			return {

				checked: false,
				phoneNum: '',
				SessionKey: '',
				openid: '',
				encryptedData: '',
				iv: '',
				userInfo: null,
				avatarUrl: '',
				nickName: '',
				from: '',
				activityId: '',
				storeId: '',
				isPopularize: false
			};
		},

		onLoad(options) {
			var that = this;
			if (options && options.from) {
				this.from = options.from;
			}

			if (options && options.scene) {
				console.log("推广++++++")
				const scene = decodeURIComponent(options.scene)
				var arr = scene.split('_')
				this.activityId = arr[0]
				this.storeId = arr[1];
				console.log(this.activityId + "活动id++++++")
				console.log(this.storeId + "门店id++++++")
				this.isPopularize = true
			}
		},

		onShow: function(e) {
			this.userInfo = app.globalData.userInfo;
			// if (this.userInfo != null && this.userInfo.phone != null && this.userInfo.phone != '') {
			// 	this.phoneNum = this.userInfo.phone
			// }
			this.getUnionid();
		},
		methods: {

			//微信小程序授权-获取SessionKey
			getUnionid: function() {
				wx.login({
					success: wxres => {
						console.log('code:' + wxres.code);

						var uri = app.globalData.config.interfaces.URL_WECHAT_GETUNIONID;

						var params = {
							"code": wxres.code,
						};
						network.get(uri, params, "form").then(res => {
							console.log(res);
							if (res.code == 200) {
								this.openid = res.data.openid;
								this.SessionKey = res.data.session_key;

							} else {
								uni.showToast({
									title: res.message,
									duration: 3000,
									icon: 'none'
								})
							}
						})
					}
				})
			},
			//微信小程序-未登录获取手机号(密)
			getPhoneNumberconfidential: function(e) {


				console.log(e.detail.errMsg == "getPhoneNumber:ok");
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					this.encryptedData = e.detail.encryptedData
					this.iv = e.detail.iv
					// this.getUserInfo()
					this.getPhoneNumber()
				}

			},
			//微信小程序-未登录获取手机号
			getPhoneNumber: function() {
				var uri = app.globalData.config.interfaces.URL_WECHAT_GETUSERINFO;
				var params = {
					"encryptedData": this.encryptedData,
					"sessionkey": this.SessionKey,
					"iv": this.iv
				};
				network.post(uri, params, "form").then(res => {
					console.log(res);
					if (res.code == 200) {
						this.phoneNum = res.data.phoneNumber;
						this.getUserWithMobile(this.phoneNum)
					} else {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				})
			},
			//获取用户信息
			getUserWithMobile: function(mobile) {
				var params = {
					"phone": mobile
				};
				const header = {};
				var that = this;
				network.get(app.globalData.config.interfaces.URL_SYS_LOAD_USERINFO, params, "form", header).then(
					res => {
						if (res.code == 200) {
							console.log("res.data++++++++++++++" + res)
							if (res.data == null) {
								console.log(1)

								if (that.isPopularize == false) {
									uni.navigateTo({
										url: "/pages/user/weChatLogin?mobile=" + mobile +
											"&isPopularize=false"
									})
								} else {
									uni.navigateTo({
										url: "/pages/user/weChatLogin?mobile=" + mobile + '&activityId=' +
											that.activityId + '&storeId=' + that.storeId +
											"&isPopularize=true"
									})

								}




							} else {
								console.log(res.data)
								app.saveUser(res.data);
								//通过获取到的用户信息登录
								that.complete()
							}
						} else {
							uni.showToast({
								title: res.message,
								duration: 3000,
								icon: 'none'
							})
						}
					}).catch(res => {
					uni.showToast({
						title: res.errMsg,
						duration: 3000,
						icon: 'none'
					})
					console.error(res);
				});
			},
			//查看并同意协议
			checkedToast: function() {
				uni.showToast({
					title: "请查看并同意协议后继续",
					duration: 3000,
					icon: 'none'
				})
			},
			//微信小程序-手机APP默认微信登录
			complete: function() {
				var that = this;
				if (!that.checked) {
					uni.showToast({
						title: "请查看并同意协议后继续",
						duration: 3000,
						icon: 'none'
					})
					return;
				}
				var uri = app.globalData.config.interfaces.URL_WECHAT_GETAPPLOGINBYAUTO;
				var params;
				var loginType = "";
				if(app.globalData.systemPlatform=="WEIXIN"){
					loginType = "0"
				}else if (app.globalData.systemPlatform=="App"){
					loginType = "1"
				}
				var loginDevice = ""
				if(app.globalData.systemVersionNum.indexOf("Android") != -1){
					loginDevice = "0"
				}
				
				if(app.globalData.systemVersionNum.indexOf("iOS") != -1){
					loginDevice = "1"
				}
				if (that.isPopularize == false) {
					params = {
						"username": app.globalData.userInfo.userName,
						"sex": app.globalData.userInfo.sex,
						"wechatCountry": '',
						"wechatProvince": app.globalData.userInfo.wechatProvince,
						"wechatCity": app.globalData.userInfo.wechatCity,
						"phoneNumber": that.phoneNum,
						"isfrom": 3, //1.pc；2.移动端；3.小程序；4.app
						"openid": that.openid,
						"unionid": that.SessionKey,
						"loginType": loginType,//登录来源(0 小程序 1 用户app 2 渠道端 3 pc)
						"loginDevice": loginDevice,//登录设备(0 Android 1 IOS)
						"loginVersion":uni.getStorageSync("version")

					}
				} else {
					params = {
						"username": app.globalData.userInfo.userName,
						"sex": app.globalData.userInfo.sex,
						"wechatCountry": '',
						"wechatProvince": app.globalData.userInfo.wechatProvince,
						"wechatCity": app.globalData.userInfo.wechatCity,
						"phoneNumber": that.phoneNum,
						"isfrom": 3, //1.pc；2.移动端；3.小程序；4.app
						"openid": that.openid,
						"unionid": that.SessionKey,
						"loginType": loginType,//登录来源(0 小程序 1 用户app 2 渠道端 3 pc)
						"loginDevice": loginDevice,//登录设备(0 Android 1 IOS)
						"loginVersion":uni.getStorageSync("version"),
						"activityId": that.activityId,
						"storeId": that.storeId
					}
				};
				console.log("1头像" + that.avatarUrl)
				console.log("1昵称" + that.nickName)
				network.post(uri, params, "json").then(res => {
					console.log(res);
					if (res.code == 200) {
						app.saveToken(res.data.token);
						that.userInfo = app.globalData.userInfo
						// this.$fire.$emit("loginSuccess");
						// uni.setStorageSync("loginSuccess", "loginSuccess");
						uni.showToast({
							title: that.from
						})

						util.gotBackSourePageMini(that.from);
						uni.showToast({
							title: "登录成功",
							duration: 3000,
							icon: 'none'
						})



						if (that.isPopularize == true) {
							setTimeout(() => {
								uni.switchTab({
									url: "/pages/community/community",
									fail(err) {
										console.log(err, 'err')
									}
								})
							}, 500)

						}



					} else {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				})
			},
			getUserInfo: function() {
				var uri = app.globalData.config.interfaces.URL_WECHAT_GETUSERINFO;
				var params = {
					"encryptedData": this.encryptedData,
					"sessionkey": this.SessionKey,
					"iv": this.iv
				};
				network.post(uri, params, "form").then(res => {
					console.log(res);
					if (res.code == 200) {
						this.phoneNum = res.data.phoneNumber;




					} else {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				})
			},



			//获取SessionKey
			getUnionid: function() {
				wx.login({
					success: wxres => {
						console.log('code:' + wxres.code);
						var uri = app.globalData.config.interfaces.URL_WECHAT_GETUNIONID;
						var params = {
							"code": wxres.code,
						};
						network.get(uri, params, "form").then(res => {
							console.log(res);
							if (res.code == 200) {
								this.openid = res.data.openid;
								this.SessionKey = res.data.session_key;
								uni.setStorageSync('openId', this.openid)

							} else {
								uni.showToast({
									title: res.message,
									duration: 3000,
									icon: 'none'
								})
							}
						})
					}
				})
			},




			onCheckChange: function(e) {
				this.checked = e.detail.value.length > 0;
			},
			agreementTouched: function(e) {
				uni.navigateTo({
					url: '/pages/user/agreement'
				});
			},
			privateTouched: function(e) {
				uni.navigateTo({
					url: '/pages/user/private'
				});
			},



		}
	}
</script>

<style>
	.container {
		background: top left no-repeat;
		background-image: url('@appstatic/my/my_icon_background.png');
		background-size: 270px, 357.43px;
		width: 100vw;
		height: 100vh;
	}

	/*渐变*/
	.cover {
		width: 100%;
		height: 217.38px;
		background: linear-gradient(180deg, #EA5404 0%, rgba(234, 84, 4, 0) 100%);
		opacity: 0.2;
		position: absolute;
		z-index: 1;
	}

	.white-cover {
		width: 100%;
		height: 297.38px;
		background: linear-gradient(0deg, #FFFFFF 0%, rgba(255, 255, 255, 0) 100%);
		position: absolute;
		z-index: 2;
	}

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.row {
		display: flex;
		flex-direction: row;
		align-items: center;
		border-bottom: 1px solid #ECEDEE;
		height: 42px;
		width: 327px;
		margin: auto;
		font-size: 16px;
		font-weight: 400;
		line-height: 22.4px;
		color: #ADB0AE
	}

	.logo_new {
		margin-top: 0px;
		display: flex;
		justify-content: center;
		flex-direction: column;
	}

	.logo_image_new {
		width: 72px;
		height: 86.33px;
		margin-left: 40px;
		margin-top: 40px;
	}

	.register-form-input {
		font-size: 28rpx;
		color: #4F4F4F;
		height: 40rpx;
		line-height: 40rpx;
		padding-left: 0rpx;
		/* text-align: right; */
		width: calc(100% - 48px - 14px - 11px - 32px);
	}

	.shortinput {
		width: calc(100% - 20rpx - 173rpx)
	}

	.canvas {
		width: 85px;
		background-color: #f7faff;
	}

	.login-image {
		width: 100%;
		height: 420rpx;
	}

	.register-image>image {
		margin-left: calc((100% - 353rpx) / 2);
		margin-top: 85rpx;
		width: 353rpx;
		height: 238rpx;
	}

	.register-button {
		line-height: 80rpx;
		height: 42px;
		width: 335px;
		margin: auto;
		border: none;
		border-radius: 2px;
		font-size: 16px;
		font-weight: 400;
		margin-top: 56px;
	}

	.register-button-uncomplete {
		line-height: 80rpx;
		height: 42px;
		width: 335px;
		margin: auto;
		border: none;
		border-radius: 2px;
		font-size: 16px;
		font-weight: 400;
		margin-top: 56px;
	}

	.logo_mode {
		width: 40px;
		height: 40px;
		margin: 10px;
	}

	button.register-button[plain] {
		border: 0;
		background-color: #EA5404;
		color: #ffffff;
		font-size: 16px;
		font-weight: 400;
	}

	button.register-button-uncomplete[plain] {
		border: 0;
		background-color: #EDEDED;
		color: #ADB0AE;
		font-size: 16px;
		font-weight: 400;
	}

	.footOpration {
		border-bottom: 0px;
		height: 75rpx;
		margin-top: 0rpx;
		font-size: 26rpx;
		display: flex;
		justify-content: space-between;
	}

	.logo {
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;
	}

	.logo_image {
		height: 100rpx;
		width: 282rpx;
		margin-top: 152rpx;
	}

	.logo_welcome {
		color: #333333;
		font-size: 36rpx;
		margin-top: 40rpx;
		margin-bottom: 40rpx;
	}

	.area_code {
		color: #333333;
		font-size: 28rpx;
		width: 20%;
	}

	.phcolor {
		color: #BDBDBD;
		font-size: 16px;
		font-weight: 400;
	}

	.ph_msgcolor {
		color: #BDBDBD;
		font-size: 16px;
		font-weight: 400;
	}

	.text {
		font-size: 10px;
	}

	.buttom-logo {
		height: 287px;
		width: 323px;
		left: 52px;

	}

	.jurisdiction {
		left: 100rpx;
		display: flex;
		align-items: center;
		flex-direction: row;
		justify-content: center;
		font-size: 10px;
		margin-top: 20px;

	}

	.checkbox-test {
		font-size: 12px;
		color: #ADB0AE;
	}

	.button_row::after {
		border: none;
	}

	/* .button_row {
		background-color: #fff;
		display: flex;
		flex-direction: row;
		align-items: center;
		height: 42px;
		width: 327px;
		margin: auto;
		font-size: 16px;
		font-weight: 400;
		line-height: 22.4px;
		color: #ADB0AE;
		border-bottom: 1px solid #ECEDEE;
		border-radius: 0;
		padding-left: 0px;
		padding-right: 0px;
	} */

	.button_row {
		width: calc(100% - 40px);
		height: 42px;
		line-height: 42px;
		margin-left: 20px;
		margin-right: 20px;
		color: #ffffff;
		border-radius: 2px;
		font-size: 16px;
		text-align: center;
		background: #EA5404;
		border: 1px solid #EA5404;
		/* position: absolute; */
		/* bottom: 44px; */
		box-sizing: border-box;
	}

	.test1 {
		font-family: 'PingFang SC';
		font-style: normal;
		font-weight: 400;
		font-size: 14px;
		line-height: 20px;
		display: flex;
		align-items: center;
		color: #000000;
	}
</style>
